define(function (require) {
var zsApp= require('app');  
require('selectCache');
zsApp.controller('terminalCtrl', ['$scope', '$http', '$timeout', 'selectCache', 'promptCache', function($scope, $http, $timeout, selectCache, promptCache) {

	// pagination
	$scope.paginationConf = {
        currentPage     : 1,
        totalItems      : 10,
        itemsPerPage    : 10,
        pagesLength     : 15,
        perPageOptions  : [10, 20, 30, 40, 50],
        onChange: function() { 
        	var self = this,
	        	pagingData = {
	        		pageNumber  : self.currentPage,  
					pageSize 	: self.itemsPerPage,  
	        	};	
			$http
			({
				url  	: $scope.zsUrl+'digitalhome-boss/product/terminals/getList',
				method 	: 'POST',
				data 	:  {
                    pageNumber  : self.currentPage,  
                    pageSize    : self.itemsPerPage,  
                }

			}).success(function(res) {

                if(res.result_code != '0000') {
                    $scope.isNoData = false;
                    return false;
                }
                
                // 表格数据
                $scope.tabVal = res.result;
                angular.forEach($scope.tabVal, function(obj, i) {
                    // 遍历每条数据对应的ID
                    $scope.dataID[i]   = obj.terminalId;
                    // 遍历每条数据对应的编码
                    $scope.dataCode[i] = obj.terminalCode;
                });
                
                // 总共有多少条数据
                self.totalItems = res.length;

            }).error(function(err) {
                console.log(err);
            });
        }
    };

    // newlyAdded
    $scope.submitNewlyAdded = function() {
    	
    	var newlyAddedData = {
    		terminalCode	: $scope.newlyAdded_terminalCode,
    		terminalName	: $scope.newlyAdded_terminalName,
    		description		: $scope.newlyAdded_description,
    	};

    	$http
    	({
    		url  	: $scope.zsUrl+'digitalhome-boss/product/terminals/add',
			method 	: 'POST',
			data 	: newlyAddedData,

    	}).success(function(res) {
    		
            if( res.result_code != '0000' ) {
                promptCache.errorPrompt(res.message);
                return false;
            }

            promptCache.successPrompt(res.message);
            reloadTable();

    	}).error(function(err) {
            promptCache.errorPrompt('新增失败！');
            return false;
    	});
    };

    // details
    $scope.details = function(i) {

    	$http
    	({
    		url  	: $scope.zsUrl+'digitalhome-boss/product/terminals/getTerminalsInfo/'+$scope.dataCode[i],
			method 	: 'GET',

    	}).success(function(res) {
    		
    		if( '0000' != res.result_code ) {
    			return false;
    		}

            $scope.details_terminalCode = res.result.terminalCode;
            $scope.details_terminalName = res.result.terminalName;
            $scope.details_description  = res.result.description;

    	}).error(function(err) {
            // console.log(err);
    	});
    };

    // edit: show data
    $scope.edit = function(i) {

    	$scope.index = i;

    	$http
    	({
    		url  	: $scope.zsUrl+'digitalhome-boss/product/terminals/getTerminalsInfo/'+$scope.dataCode[i],
			method 	: 'GET',

    	}).success(function(res) {

    		if( res.result_code != '0000' ) {
    			return false;
    		}
    		
    		$scope.edit_terminalCode = res.result.terminalCode;
    		$scope.edit_terminalName = res.result.terminalName;
    		$scope.edit_description  = res.result.description;

    	}).error(function(err) {

    	});
    };

    // edit: submit data
    $scope.submitEdit = function() {

    	var editData = {
            terminalId      : $scope.dataID[$scope.index],
    		terminalCode 	: $scope.edit_terminalCode,
    		terminalName 	: $scope.edit_terminalName,
    		description 	: $scope.edit_description,
    	};

    	$http({
    		url  	: $scope.zsUrl+'digitalhome-boss/product/terminals/modify',
			method 	: 'POST',
			data 	: editData,

    	}).success(function(res) {
		  
            if( res.result_code != '0000' ) {
                promptCache.errorPrompt(res.message);
                return false;
            }

            promptCache.successPrompt(res.message);
            reloadTable();  

    	}).error(function(err) {
    		promptCache.errorPrompt('修改失败！');
            return false;
    	});
    };

    // delete
    $scope.getDataID = function() {

    	// data length
    	var len = $scope.dataID.length;
    	// param id
    	var whichId = 'terminalId';
    	// param
    	var whichUrl = 'product/terminals/delete';

    	// multiple selections 
        $scope.selectAll= function (bool, obj) {
            selectCache.selectAll(bool, $scope.dataID);
        };

        //  Single or multiple selections 
        $scope.select= function (selectObj, bool) {   
         	var selectId = selectObj.terminalId;        
            selectCache.select(selectId, bool, len);    
        }; 

        // check
        $scope.check= function () {
            selectCache.check();
        };

        // delete
        $scope.delete= function () { 
            selectCache.delete(whichId, whichUrl);
            reloadTable();
        };
    };

    // reload table
    function reloadTable() { 
        $timeout(function() {
            $scope.paginationConf.onChange();
            $scope.master = false;
        },1500);
    };
}])
.directive('terminalNewlyadded', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/openPlatform/terminal/newlyAdded.html',
		replace			: true,
	}
})
.directive('terminalDetails', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/openPlatform/terminal/details.html',
		replace			: true,
	}
})
.directive('terminalEdit', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/openPlatform/terminal/edit.html',
		replace			: true,
	}
})
.directive('terminalDelete', function() {
    return {
        restrict        : 'E',
        templateUrl     : 'system/openPlatform/terminal/delete.html',
        replace         : true,
    }
})
.directive('terminalTable', function() {
	return {
		restrict		: 'E',
		templateUrl		: 'system/openPlatform/terminal/table.html',
		replace			: true,
	}
});
});
